﻿using B.T.BasicData.Domain.RBAC;
using B.T.BasicData.ErrorCode;
using B.T.BasicData.Infraeturcture;
using B.T.BasicData.Read.Api.DTOs;
using B.T.BasicData.Read.Api.RBACServices.IServices;
using Microsoft.AspNetCore.Identity;

namespace B.T.BasicData.Read.Api.RBACServices.Services
{
    public class UserServices:IUserServices
    {
        private readonly ILogger<UserServices> logger;
        private readonly IBaseRepository<Userinfo> userinfo;
        private readonly IBaseRepository<User_Role> userRoleRepo;
        private readonly IBaseRepository<RolesInfo> roleRepo;
        public UserServices(ILogger<UserServices> logger,IBaseRepository<Userinfo> userinfo, IBaseRepository<User_Role> userRoleRepo, IBaseRepository<RolesInfo> roleRepo)
        {
            this.logger = logger;
            this.userinfo = userinfo;
            this.userRoleRepo = userRoleRepo;
            this.roleRepo = roleRepo;
        }

        /// <summary>
        /// 分页查询用户信息及角色
        /// </summary>
        /// <param name="dto">查询条件</param>
        /// <returns>分页用户信息</returns>
        public async Task<ApiPaging<UserPageDto>> SraechUser(SraechUserDto dto)
        {
            logger.LogInformation("分页查询用户信息及角色操作");
            var res = new ApiPaging<UserPageDto>();
            var query = userinfo.Getlist().Where(x => !x.IsDeleted);
            if (!string.IsNullOrEmpty(dto.UserName))
            {
                query = query.Where(x => x.UserName.Contains(dto.UserName));
            }
            var totalCount = query.Count();
            var totalPage = (int)Math.Ceiling(totalCount * 1.0 / dto.PageSize);
            var pageData = query.OrderByDescending(x => x.UserId)
                .Skip((dto.PageIndex - 1) * dto.PageSize)
                .Take(dto.PageSize)
                .ToList();

            var userIds = pageData.Select(x => x.UserId).ToList();
            var userRoles = userRoleRepo.Getlist().Where(x => userIds.Contains(x.UserId) && !x.IsDele).ToList();
            var roleIds = userRoles.Select(x => x.RolesId).Distinct().ToList();
            var roles = roleRepo.Getlist().Where(x => roleIds.Contains(x.RolesId) && !x.IsDeleted).ToList();

            var data = pageData.Select(u => new UserPageDto
            {
                UserId = u.UserId,
                UserName = u.UserName,
                Password = u.Password,
                status = u.status,
                Serialnumber = u.Serialnumber,
                CreateTime = u.CreateTime,
                IsDeleted = u.IsDeleted,
                RoleNames = userRoles.Where(ur => ur.UserId == u.UserId)
                                    .Join(roles, ur => ur.RolesId, r => r.RolesId, (ur, r) => r.RoleName)
                                    .ToList()
            }).ToList();

            res.TotalCount = totalCount;
            res.TotalPage = totalPage;
            res.PageData = data;
            res.code = ApiREunm.Ok;
            res.msg = "查找用户信息成功";
            return res;
        }
    }
}
